package com.vip.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vip.base.service.ISysPermService;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.Result;
import com.vip.common.response.Results;
import com.vip.database.dto.perm.AddPermDTO;
import com.vip.database.dto.perm.GetPermDTO;
import com.vip.database.entity.SysPerm;
import com.vip.database.mapper.SysPermMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.MessageFormat;
import java.util.List;

/**
 * 权限表 服务实现类
 *
 * @author LEON
 * @since 2020-03-28
 */
@Service
public class SysPermServiceImpl extends ServiceImpl<SysPermMapper, SysPerm> implements ISysPermService {
    @Autowired
    private SysPermMapper permMapper;

    @Override
    public Result<String> addNewPerm(AddPermDTO dto) {
        try {
            Result<Boolean> isExistResult =  permIsExist(dto.getCode());
            if(isExistResult.isSuccess()){
                if(!isExistResult.getData()){
                    SysPerm perm = new SysPerm();
                    BeanUtils.copyProperties(dto, perm);
                    permMapper.insert(perm);
                    return Results.newSuccessResult(perm.getId(), "添加成功");
                }
            }
            return Results.newFailResult(isExistResult.getErrorCode(), isExistResult.getDescription());
        } catch (Exception e) {
            log.error("新增权限出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "添加失败");
    }

    @Override
    public Result<Boolean> permIsExist(String perm) {
        try {
            // 如果查询结果为空则表示权限不存在，否则存在
            if (permMapper.getPermIsExist(perm)) {
                return Results.newSuccessResult(true, MessageFormat.format("权限编码{0}已存在", perm));
            } else {
                return Results.newSuccessResult(false);
            }
        } catch (Exception e) {
            log.error("查询权限出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<List<GetPermDTO>> getPermList(Integer pageNo, Integer pageSize) {
        try {
            PageHelper.startPage(pageNo, pageSize);
            List<GetPermDTO> result = permMapper.getPermList();
            PageInfo<GetPermDTO> pageInfo = new PageInfo<>(result);
            return Results.newSuccessResult(pageInfo.getList(), "查询成功");
        } catch (Exception e) {
            log.error("获取权限列表数据出错！", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<GetPermDTO> getPermDetailsByPermId(String permissionId) {
        try {
            GetPermDTO result = permMapper.getPermDetailsByPermId(permissionId);
            return Results.newSuccessResult(result, "查询成功");
        } catch (Exception e) {
            log.error("获取权限详情出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "查询失败");
    }

    @Override
    public Result<String> updatePerm(AddPermDTO dto) {
        try {
            SysPerm perm = new SysPerm();
            BeanUtils.copyProperties(dto, perm);
            permMapper.updateById(perm);
            return Results.newSuccessResult(perm.getId(), "更新成功");
        } catch (Exception e) {
            log.error("更新用户出错", e);
        }
        return Results.newFailResult(ErrorCode.DB_ERROR, "更新失败");
    }
}
